import React, { Component } from 'react';
import { inject, observer } from 'mobx-react';
import {
  WeaDialog, WeaLocaleProvider, WeaRightMenu, WeaTab, WeaTools,
  WeaFormItem, WeaSearchGroup,
} from 'ecCom';
import { WeaTableNew, WeaSwitch } from 'comsMobx';

import { Button } from 'antd';
import * as dialogTypes from '../../constants/OdocCompsDialogType';
import * as mobx from 'mobx';

const { getLabel } = WeaLocaleProvider;
const { WeaTable } = WeaTableNew;

@inject('adjustLogStore', 'odocCompsStore')
@observer
class AdjustLog extends Component {
  constructor(props) {
    super(props);
    this.state = {
      height: 500,
    };
  }
  componentWillReceiveProps(nextProps) {
    if (this.props.visible != nextProps.visible && nextProps.visible) {
      this.loadData();
    }
  }

  componentWillMount() {
    this.loadData();
  }

  render() {
    const { odocCompsStore = {} } = this.props;
    let visible = false;
    try {
      let { dialogVisible } = odocCompsStore;
      visible = dialogVisible.get(dialogTypes.ODOC_ADJUST_LOG);
    } catch (e) {
    }
    const { height } = this.state;

    let dialogparams = {
      style: { width: 900, height },
      visible,
      title: getLabel(25990, '调整日志'),
      onCancel: this.onCancel,
      buttons: this.getButtons(),
      icon: 'icon-coms-Flow-setting',
      iconBgcolor: '#008572',
    };
    return (
      <div>
        <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@uallva`} {...dialogparams}>
          <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@wq6tzb`} datas={this.getRightMenu()}>
            {this.getTableCom()}
          </WeaRightMenu>
        </WeaDialog>
      </div>
    );
  }
  loadData = () => {
    const { odocCompsStore = {}, adjustLogStore } = this.props;
    let params = {};
    const { initLoad } = adjustLogStore;
    try {
      let { requestParam } = odocCompsStore;
      requestParam = mobx.toJS(requestParam);
      delete requestParam.markInfo;
      params = { ...params, ...requestParam };
    } catch (e) {
      // 异常处理，用于热部署
      params = { requestid: 12623619, workflowid: 51022, nodeid: 42530 };
    }
    initLoad(params);
  }

  getTableCom = () => {
    const { showSearchAd = false } = this.state;
    const { adjustLogStore } = this.props;

    let tabparams = {
      searchType: ['advanced'],
      advanceHeight: 200,
      buttonsAd: this.getButtonsAdQuick(),
      showSearchAd,
      setShowSearchAd: bool => this.setState({ showSearchAd: bool }),
      searchsAd: this.getSearchs(),
    };

    const { tableStore, loadData } = adjustLogStore;

    let tableparams = {
      className: 'adjust_table',
      comsWeaTableStore: tableStore,
      scroll: { y: (370) },
      expiredRefreshFun: loadData,
    };
    return (<div>
      <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@gi13s0`} {...tabparams} />
      {tableStore && <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@1qmgzx`} {...tableparams} />}
    </div>);
  }

	/**
	 * 高级搜索框信息
	 */
  getSearchs = () => {
    const { adjustLogStore } = this.props;
    const { conditions, form } = adjustLogStore;
    let groups = [];
    if (form.isFormInit) {
      conditions.map((group, index) => {
        groups.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@prdylq@${index}`}
          needTigger={false}
          showGroup={group.defaultshow}
          items={group.items.map(field => ({
            com: (
              <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@krf0k0@${field.domkey[0]}`}
                label={field.label}
                labelCol={{ span: field.labelcol }}
                wrapperCol={{ span: field.fieldcol }}
              >
                <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@c8f1o1@${field.domkey[0]}`} fieldConfig={field} form={form} />
              </WeaFormItem>
            ),
            col: field.conditionType.toUpperCase() == WeaTools.types.CASCADER ? 1 : 2,
          }))}
        />);
      });
    }
    return groups;
  }
  getButtonsAdQuick = () => {
    const { adjustLogStore } = this.props;
    const { form, loadData } = adjustLogStore;
    return [
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@qp67rl@search`}
        type="primary"
        onClick={() => {
          loadData();
          this.setState({ showSearchAd: false });
        }}
      >
        {getLabel(388113, '搜索')}
      </Button>,
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@mzzfcj@reset`}
        type="ghost"
        onClick={() => {
          form.resetForm();
        }}
      >
        {getLabel(2022, '重置')}
      </Button>,
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@uwroko@cancel`} type="ghost" onClick={() => this.setState({ showSearchAd: false })}>
        {getLabel(201, '取消')}
      </Button>,
    ];
  }

  onCancel = () => {
    const { onClose, adjustLogStore } = this.props;
    this.setState({ visible: false, showSearchAd: false });
    const { destory } = adjustLogStore;
    destory();
    onClose && typeof onClose == 'function' && (onClose(dialogTypes.ODOC_FILL_REMARK_DIALOG));
  }

	/**
	 * 获取右键菜单
	 * @returns {Array}
	 */
  getRightMenu() {
    let btns = [];
    const showAllBtns = this.allBtns();
    showAllBtns && showAllBtns.map((item) => {
      btns.push({ ...item });
    });
    return btns;
  }

	/**
	 * dialog获取底部按钮
	 * @returns {Array}
	 */
  getButtons = () => {
    let buttons = [];
    const showAllBtns = this.allBtns();
    showAllBtns && showAllBtns.map((item, index) => {
      buttons.push([<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@7di5hq@${index}`} {...item}>{item.content}</Button>]);
    });
    return buttons;
  }
	/**
	 * 统一管理btns
	 * @returns {{key: string, icon: *, content: *, onClick: Index.goSave, type: string}[]}
	 */
  allBtns = () => {
    const { pluginbuttons = [] } = this.props;
    let showAllBtns = [
      {
        key: 'close',
        icon: <i className="icon-coms-go-back" />,
        content: getLabel(201, '取消'),
        onClick: this.onCancel,
        type: 'ghost',
      },
    ];
    pluginbuttons && pluginbuttons.length >= 1 && (showAllBtns = showAllBtns.concat(pluginbuttons));
    return showAllBtns;
  }
}

export default AdjustLog;
